
	// base info 基本信息
	var BI = {
		bRecord: {
			id 	 : '#bRecord-dg',
			name : 'bRecord',
			url  : {query: 'list', save: 'save', del: 'singleDel', batchDel: '', renewBorrow: 'renewBorrow', batchRenewBorrow: 'batchDel'},
			dlg	 : {titleInsert : '新添借阅信息', titleUpdate: '借阅信息修改', 
					insertRule: function(){},
					updateRule: function(){},
					dataProcess: function(row){
						$('#bookId').textbox('setText', row.bookName);
						$('#readerId').textbox('setText', row.readerName);
					}},
					
			toolbar : {id: '#bRecord-tb'},
		},
		// 图书信息
		reader: {
			id 	 : '#reader-dg',
			name : 'reader',
			url  : {query: '../readerManage/list', insert: '', update: '', del: '', batchDel: ''},
			dlg	 : {title : '读者信息', width: 600},
			toolbar : {id: '#reader-tb'},
		},
		// 图书子表信息
		sub: {
			id	 : [],
			url  : {query: '../bookManage/detailList', insert: '', update: '', del: ''},
			objs : [], // 子表格数组
		},
		// 类型信息
		book: {
			id	 : '#book-dg',
			name : 'book',
			url  : {query: '../bookManage/list', insert: '', update: '', del: ''},
			dlg	 : {title : '图书信息', width: 600},
			toolbar : {id: '#book-tb'},
		},
	};
	// 借阅记录表格对象
	BI.bRecord.obj 	 = new easyUI.extend.TABLE(BI.bRecord.name, BI.bRecord.url);
	// 读者表格对象
	BI.reader.obj 	 = new easyUI.extend.DLAG_TABLE(BI.reader.dlg.title, BI.reader.name, BI.reader.url, BI.reader.dlg.width);
	// 类型弹出框表格对象
	BI.book.obj  = new easyUI.extend.DLAG_TABLE(BI.book.dlg.title, BI.book.name, BI.book.url, BI.book.dlg.width);

	function textTable(id, obj){
		$(id).textbox({
			editable: false,
			buttonText: '···',
			buttonAlign: 'right',
			onClickButton: function(){
				obj.showDlg();
			}
		});
	}
	
	function textSearch(id, tableId){
		$(id).textbox({
			buttonIcon: 'icon-search',
			buttonAlign: 'right',
			onClickButton: function(){
				$(tableId).datagrid('reload', {s_name:$(this).textbox('getText')});
			}
		});
	}
	
	// 初始化控件
	function init(){
		textTable('#s_bookId', BI.book.obj);
		textTable('#bookId', BI.book.obj);
		textTable('#s_readerId', BI.reader.obj);
		textTable('#readerId', BI.reader.obj);
		textSearch('#bookName', BI.book.id);
		textSearch('#readerName', BI.reader.id);
	}

	$(document).ready(function(){
			
			// 初始化datagrid
			$(BI.bRecord.id).datagrid({ 
		        striped: true,
		        rownumbers: true,
		        pageSize: 10,
		        idField: 'id', //这个idField必须指定为输出的id，输出的是Id就必须是Id，不能小写
		        sortName: 'id',
		        sortOrder: 'desc',
		        width: '100%', //宽度
		        height: 500, //高度
		        url: BI.bRecord.url.query, //请求路径
		        singleSelect: true, //单选
		        fitColumns: true, //设置为true将自动使列适应表格宽度以防止出现水平滚动。
		        pagination: true, //启用分页
		        checkOnSelect: false,
		        selectOnCheck: false,
				resizable: false,
				
		        columns: [
		            [
		            	{ field: 'check',title:'', width:10, checkbox:true},
		                { field: 'id', title:'编号', align:'center', width: 70, hidden: true},
		                { field: 'bookId', title:'图书Id', align:'center', width: 70, hidden: true},
		                { field: 'bookName', title:'图书', align:'center', width: 70},
		                { field: 'readerId', title:'读者Id', align:'center', width: 70, hidden: true},
		                { field: 'readerName', title:'读者', align:'center', width: 70},
		                { field: 'managerId', title:'管理员Id', align:'center', width: 70, hidden: true},
		                { field: 'managerName', title:'管理员', align:'center', width: 70},
		                { field: 'borrowDate', title:'借阅时间', align:'center', width: 100, formatter:
		                	function(value, row, index){
		                	return timeStampToStr(value);
		                }},
		                { field: 'restoreDate', title:'归还时间', align:'center', width: 70, formatter:
		                	function(value, row, index){
		                	return timeStampToStr(value);
		                }},
		                { field: 'renewBorrowNum', title:'续借次数', align:'center', width: 70},
		                { field: 'status', title:'状态', align:'center', width: 70, formatter:
							function(value, row, index){
								if(value == 0){
									return '借阅';
								} else if (value == 1){
									return '超期';
								} else if (value == -1){
									return '归还';
								}
							}},
						{ field: 'renewBorrow', title:'续借办理', align:'center', width: 70, formatter:
							function(value, row, index){
								if (value != -1){
									if (row.status != -1){
										return '<a class="editor-btn purple" href="javaScript:void(0);" onclick="BI.bRecord.obj.renewBorrow(BI.bRecord.url.renewBorrow)">续借</a>';
									} else {
										return '<a class="editor-btn gray" href="javaScript:void(0);">续借</a>';
									}
								} else {
									return '<span class="span-gray">已达上限</span>';
								}
							}},
						{ field: 'operation', title:'操作', align:'center', width: 70, formatter:
							function(value, row, index){
								if (row.status != -1){
									return '<a class="editor-btn green" href="javaScript:void(0);" onclick="BI.bRecord.obj.update(BI.bRecord.dlg.titleUpdate, BI.bRecord.dlg.updateRule, BI.bRecord.dlg.dataProcess)">修改</a><a class="editor-btn red" href="javaScript:BI.bRecord.obj.del(\'归还《' + row.bookName + '》书籍吗？\')">归还</a>';
								} else {
									return '<span class="span-gray">已归还</span>';
								}
							}},
		            ]
		        ],
		        
		        onLoadSuccess: function(data){//加载完毕后获取所有的checkbox遍历
			        if (data.rows.length > 0) {
			        	$(BI.bRecord.id).datagrid('clearChecked');
			            //循环判断操作为新增的不能选择
			            for (var i = 0; i <= data.rows.length - 1; i++) {
			            	data.rows[i].restoreDate = timeStampToStr(data.rows[i].restoreDate);
			            	data.rows[i].borrowDate = timeStampToStr(data.rows[i].borrowDate);
			                //根据isFinanceExamine让某些行不可选
			                if (data.rows[i].status == -1) {
			                    $("input[type='checkbox']")[i + 1].disabled = 'disabled';
			                }
			            }
			            console.log(data);
			            // 重写全选事件
			            $('.datagrid-header-check > input[type="checkbox"]').on('click', function(e){
			            	if ($(this).is(':checked')){
			            		for (var one in data.rows){
						        	if (data.rows[one].status != -1){
						        		$(BI.bRecord.id).datagrid('checkRow', one);
						        	}
						        }
			            	} else {
			            		for (var one in data.rows){
						        	if (data.rows[one].status != -1){
						        		$(BI.bRecord.id).datagrid('uncheckRow', one);
						        	}
						        }
			            	}
			            });
			        }
			    },
			    onClickRow: function(rowIndex, rowData){
			        //加载完毕后获取所有的checkbox遍历
			        $("input[type='checkbox']").each(function(index, el){
			            //如果当前的复选框不可选，则不让其选中
			            if (el.disabled == true) {
			                $(BI.bRecord.id).datagrid('unselectRow', index - 1);
			            }
			        })
			    },
			    onCheckAll: function(rows){
			        // 重写全选方法
			        for (var one in rows){
			        	if (rows[one].status == -1){
			        		$(BI.bRecord.id).datagrid('uncheckRow', one);
			        	}
			        }
			        $(".datagrid-header-check > input[type='checkbox']")[0].checked = true;
			    },
		               
		        toolbar: $(BI.bRecord.toolbar.id),
		    });
			
			
			// 初始化datagrid
			$(BI.reader.id).datagrid({ 
		        striped: true,
		        rownumbers: true,
		        pageSize: 10,
		        idField: 'id', //这个idField必须指定为输出的id，输出的是Id就必须是Id，不能小写
		        sortName: 'id',
		        sortOrder: 'desc',
		        width: '100%', //宽度
		        height: 300, //高度
		        url: BI.reader.url.query, //请求路径
		        singleSelect: true, //单选
		        fitColumns: true, //设置为true将自动使列适应表格宽度以防止出现水平滚动。
		        pagination: true, //启用分页
		        checkOnSelect: false,
		        selectOnCheck: false,
				resizable: false,
				queryParams: {s_name:''},
		        columns: [
		            [
		                { field: 'id', title:'编号', align:'center', width: 70, hidden: true},
		                { field: 'userInfoId', title:'用户ID', align:'center', width: 70, hidden: true},
		                { field: 'name', title:'用户名', align:'center', width: 70},
		                { field: 'account', title:'账号', align:'center', width: 100},
		                { field: 'IDCard', title:'身份证', align:'center', width: 100},
		                { field: 'borrowBooksNum', title:'借阅册数', align:'center', width: 70},
		                { field: 'borrowNum', title:'借阅数量', align:'center', width: 70, hidden: true},
		                { field: 'categoryId', title:'类型Id', align:'center', width: 70, hidden: true},
		                { field: 'category', title:'类型', align:'center', width: 70},
		                { field: 'balance', title:'余额', align:'center', width: 70, formatter:
							function(value, row, index){
								return "￥" + value;
							}},
							
		                { field: 'phone', title:'手机号', align:'center', width: 70, formatter:
							function(value, row, index){
								if(value == "" || value == null){
									return '<span class="editor-null" ></span>';
								}
								return value;
							}},
							
		                { field: 'birthday', title:'出生日期', align:'center', width: 70, formatter:
							function(value, row, index){
								if(value == "" || value == null){
									return '<span class="editor-null" ></span>';
								}
								return value;
							}},
		            ]
		        ],
		        
		        onSelect: function(index, row){
	        		if (row){
                        $('#readerId').textbox('setValue', row.id);
                        $('#readerId').textbox('setText', row.name);
                        BI.reader.obj.closeDlg();
                    }else{
                        $.messager.alert("系统提示","请选择一条记录！");
                        return;
                    }
	        	},
		      
		        toolbar: $(BI.reader.toolbar.id),
		    });
		   
			// 初始化datagrid
			$(BI.book.id).datagrid({ 
		        striped: true,
		        rownumbers: true,
		        pageSize: 10,
		        idField: 'Id', //这个idField必须指定为输出的id，输出的是Id就必须是Id，不能小写
		        sortName: 'Id',
		        sortOrder: 'desc',
		        width: '100%', //宽度
		        height: 465, //高度
		        url: BI.book.url.query, //请求路径
		        singleSelect: true, //单选
		        fitColumns: true, //设置为true将自动使列适应表格宽度以防止出现水平滚动。
		        pagination: true, //启用分页
				resizable: false,
				rowStyler:function(index,row){   
			        if (row.status == -1){   
		            		return 'background-color:#ccc;color:#878787;';   
			        	}   
			   		},
//				loadFilter: function(data){
//	        		if(!data.rows){
//	        			data.rows = [];
//	        		}
//	        		return data;
//				},
		        columns: [
		            [
		                { field: 'id', title:'编号', align:'center', width: 70, hidden: true},
		                { field: 'name', title:'书名', align:'center', width: 70},
		                { field: 'author', title:'作者', align:'center', width: 70},
		                { field: 'publishDate', title:'出版日期', align:'center', width: 70},
		                { field: 'publisherId', title:'出版社id', align:'center', width: 70, hidden: true},
		                { field: 'publisherName', title:'出版社', align:'center', width: 70},
		                { field: 'categoryId', title:'图书类型', align:'center', width: 70, hidden: true},
		                { field: 'categoryName', title:'类型', align:'center', width: 70},
		                { field: 'ISBN', title:'书码', align:'center', width: 70},
		                { field: 'price', title:'价格', align:'right', width: 70, formatter:
							function(value, row, index){
								return "￥" + value;
							}},
		            ]
		        ],
		    	view: detailview,
		    	detailFormatter: function(index,row){
		    		return '<div><table id="sub_' + index + '"></table></div>';
		    	},
		    	onExpandRow: function(index,row){
		    		row.editing = false;
		    		// 单元格编辑标识
		    		easyUI.extend.TABLE_CELL.prototype.sign = false;
		    		// 子表格Id
		    		BI.sub.id[index] = '#sub_' + index;
		    		// 图书子表格对象
					BI.sub.objs[index] = new easyUI.extend.TABLE_SUB(BI.sub.id[index], BI.sub.url);
		    		var ddv = $(this).datagrid('getRowDetail',index).find(BI.sub.id[index]);
		    		var tableIndex = index;
		    		
		    		ddv.datagrid({
		    			url: BI.sub.url.query,
		    			width: '80%',
		    			align: 'center',
		    			fitColumns: true,
		    			singleSelect: true,
		    			rownumbers: true,
		    			loadMsg: '努力加载中。。。。',
		    			height: 'auto',
		    			resizable: false,
		    			rowStyler:function(index,row){   
					        if (row.status == -1){   
				            		return 'background-color:#ccc;color:#878787;';   
					        	}   
					   		},
		    			queryParams: {publishDate: row.publishDate, ISBN: row.ISBN, authority: 99},
		    			columns:[[
									{ field: 'id', title:'编号', align:'center', width: 70},
									{ field: 'name', title:'书名', align:'center', width: 70},
									{ field: 'status', title:'图书状态', align:'center', width: 70, formatter: 
										function(value,row,index){
											if (value == 0){
												return "外借";
											} else if (value == 1){
												return "在库";
											} else if (value == -1){
												return "注销";
										}},
										editor:{
											type:'combobox',
											options:{
												editable: false,
												valueField:'value',
												textField:'name',
												data:[{value:1, name:'在库'}, {value:0, name:'外借'}],
												required:true
											}
										}},
		    			]],
		    			onBeforeEdit:function(index,row){
		    				row.editing = true;
		    				easyUI.extend.TABLE_CELL.prototype.sign = true;
		    				BI.sub.objs[tableIndex].tableCell.updateActions(index);
		    			},
		    			
		    			onAfterEdit:function(index,row){
		    				row.editing = false;
		    				easyUI.extend.TABLE_CELL.prototype.sign = false;
		    				BI.sub.objs[tableIndex].tableCell.updateActions(index);
		    			},
		    			
		    			onCancelEdit:function(index,row){
		    				row.editing = false;
		    				easyUI.extend.TABLE_CELL.prototype.sign = false;
		    				BI.sub.objs[tableIndex].tableCell.updateActions(index);
		    			},
		    			
		    			onResize:function(){
		    				$(BI.book.id).datagrid('fixDetailRowHeight',index);
		    			},
		    			onLoadSuccess:function(){
		    				setTimeout(function(){
		    					$(BI.book.id).datagrid('fixDetailRowHeight',index);
		    				},0);
		    			},
		    			onSelect: function(index, row){
		            		if (row){
		                        $('#bookId').textbox('setValue', row.id);
		                        $('#bookId').textbox('setText', row.name);
		                        BI.book.obj.closeDlg();
		                	}else{
		                        $.messager.alert("系统提示","请选择一条记录！");
		                        return;
		               		}
		            	},	
		    		});
		    		
		    		$(BI.book.id).datagrid('fixDetailRowHeight',index);
		    		
//		    		// 表格行样式
//		    		$(BI.sub.id[tableIndex]).datagrid({ 
//						rowStyler:function(index,row){   
//					        if (row.status == -1){   
//				            		return 'background-color:#ccc;color:#878787;';   
//					        	}   
//					   		}   
//						});
		    	},
		               
		        toolbar: $(BI.book.toolbar.id),
		    });
				    
		    // 表格行样式
			$(BI.bRecord.id).datagrid({ 
				rowStyler:function(index,row){   
			        if (row.status == -1){   
		            		return 'background-color:#ccc;color:#878787;';   
			        	}   
			   		}   
			}); 
		    
		    // 初始化
		    init();
	});
	
	
	BI.bRecord.obj.batchRenewBorrow = function(){
		var rows = $(this.tableSelector).datagrid('getChecked');
			
		var ids = "";
		for (var one in rows){
			ids += rows[one].id + ",";
		}
		
		if(rows.length > 0){
			$.messager.confirm("系统提示", '确认归还所有所选图书？', function(r){
	            if (r){
	                $.ajax({
	                    type: 'POST',
	                    url: BI.bRecord.url.batchRenewBorrow,
	                    async: false,
	                    dataType: "json",
	                    data: {ids: ids},
	                    success: function (rs) {
	                        if (rs.status == 0){
	                            $.messager.alert("系统提示", rs.msg);
	            				$(BI.bRecord.id).datagrid('reload');
	                        } else {
	                            $.messager.show({
	                                title:'错误提示' + rs.status,
	                                msg:rs.msg
	                            })
	                        }
	                    }
	                });
	            }
	        });
	   	} else {
			$.messager.alert("系统提示", "请选择要删除的记录！！");
	   	}
	};

	BI.bRecord.obj.renewBorrow = function(url){
		var row = $(this.tableSelector).datagrid('getSelected');
		if(row){
			$.messager.confirm("系统提示", '是否要续借《' + row.bookName + '》这本书？', function(r){
	            if(r){
	                $.ajax({
	                    type: 'POST',
	                    url: BI.bRecord.url.renewBorrow,
	                    async: false,
	                    dataType: "json",
	                    data: row,
	                    success: function (rs) {
	                        if (rs.status == 0){
	                            $.messager.alert("系统提示", rs.msg);
	                            $(BI.bRecord.id).datagrid('reload');
	                        } else {
	                            $.messager.show({
	                                title:'错误提示' + rs.status,
	                                msg:rs.msg
	                            })
	                        }
	                    }
	                });
	            }
	        });
		}
	}
